'use strict';

module.exports = app => {
  const { INTEGER, STRING, TEXT } = app.Sequelize;
  const Blog = app.model.define('blog', {
    userId: {
      type: INTEGER,
      allowNull: false,
      comment: '外键 userId -> user.id',
    },
    content: {
      type: TEXT,
      allowNull: false,
      comment: '微博内容',
    },
    image: {
      type: STRING,
      allowNull: true,
      comment: '图片',
    },
  });
  Blog.associate = () => {
    app.model.Blog.belongsTo(app.model.User, {
      
      foreignKey: 'userId',
      targetKey: 'id',
    });
      // 和userRelation 表关联起来
      app.model.Blog.belongsTo(app.model.UserRelation, {
        foreignKey: 'userId', // 同一个字段不可以设置两个外键,这样写只是在sequelize中做关联，并不是真的设置了两个外键
        targetKey: 'followerId',
      });
      app.model.Blog.hasMany(app.model.AtRelation,{
        foreignKey:'blogId',
      })
  };
  return Blog;
};
